package com.itheima.publisher.config;

import jakarta.annotation.PostConstruct;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

@Configuration
@AllArgsConstructor
@NoArgsConstructor
public class MqConfig {


    @Autowired
    private RabbitTemplate rabbitTemplate;



    @PostConstruct
    public void init() {


        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            /****
             * 消息发送交换机成功，队列失败，这里就收到消息
             * @param returnedMessage
             */
            @Override
            public void returnedMessage(ReturnedMessage returnedMessage) {
                ReturnedMessage message = returnedMessage;
                String exchange = returnedMessage.getExchange();
                String routingKey = returnedMessage.getRoutingKey();
                int replyCode = returnedMessage.getReplyCode();
                String replyText = returnedMessage.getReplyText();
                System.out.println("message:" + message);
                System.out.println("exchange:" + exchange);
                System.out.println("routingKey:" + routingKey);
                System.out.println("replyCode:" + replyCode);
                System.out.println("replyText:" + replyText);
            }
        });
    }
}
